package game.ecosysteme;

import java.awt.Color;
import java.awt.Point;
import java.util.List;

import org.apache.log4j.Logger;

public class Vie {

	private final static Logger log = Logger.getLogger(Vie.class);

	public static final int CLAN_A = 0;
	public static final int CLAN_B = 1;
	public static final int CLAN_C = 2;
	public static final int SEXE_HOMME = 0;
	public static final int SEXE_FEMME = 1;
	public static final int AGE_MINI_REPRODUCTION = 50;
	public static final int AGE_MAX_REPRODUCTION = 150;

	private int age = 0;

	private int clan;

	private int sexe;

	private Point localite;

	private Planete planete = null;

	public Vie(int clan) {
		this.clan = clan;
		// TODO generer un sexe
		if (Math.random() < 0.5) {
			sexe = SEXE_FEMME;
		} else {
			sexe = SEXE_HOMME;
		}
	}

	public Vie(int clan, int sexe) {
		this.clan = clan;
		this.sexe = sexe;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public int getClan() {
		return clan;
	}

	public void setClan(int clan) {
		this.clan = clan;
	}

	public void bonAnniversaire() {
		if (!estMort()) {
			age++;
			if (age == 255) {
				etVoiciLaMort();
			}
		}
	}

	public void etVoiciLaMort() {
		planete.mortDe(localite);
	}

	public boolean seSentMenace() {
		return false;
	}

	public String toString() {

		StringBuffer apparence = new StringBuffer("");
		if (!estMort()) {

			if (clan == CLAN_A) {
				if (sexe == SEXE_FEMME) {
					apparence.append("a");
				} else {
					apparence.append("A");
				}
			} else if (clan == CLAN_B) {
				if (sexe == SEXE_FEMME) {
					apparence.append("b");
				} else {
					apparence.append("B");
				}
			} else if (clan == CLAN_C) {
				if (sexe == SEXE_FEMME) {
					apparence.append("c");
				} else {
					apparence.append("C");
				}
			}
			if (age < 10) {
				apparence.append("00" + age);
			} else if (age < 100) {
				apparence.append("0" + age);
			} else {
				apparence.append(age);
			}
		} else {
			apparence.append(" +- ");
		}
		return apparence.toString();
	}

	public Point getPosition() {
		return localite;
	}

	public void setPosition(Point position) {
		this.localite = position;
	}

	public int getSexe() {
		return sexe;
	}

	public void setSexe(int sexe) {
		this.sexe = sexe;
	}

	public boolean estMort() {
		if (age == 255) {
			return true;
		} else {
			return false;
		}
	}

	public void vivre() {

		// Homocide
		if (peutSeBattre()) {
			if (planete.getEmplacementVoisinLibre(localite) == null) {
				List<Vie> proches = planete.getViesVoisines(localite);
				for (int i = 0; i < proches.size(); i++) {
					if (estDuClanOppose(proches.get(i))) {
						if (combattre(proches.get(i))) {
							planete.tuer(proches.get(i).getPosition());
						} else {
							planete.tuer(getPosition());
						}
					}
				}
			}
		}

		// Reproduction
		if (estUneFemme() && peutSeReproduire()) {
			List<Vie> proches = planete.getViesVoisines(localite);
			for (int i = 0; i < proches.size(); i++) {
				Vie proche = proches.get(i);
				if (estDuSexeOppose(proche) && proche.peutSeReproduire()
						&& estDuMemeClan(proche)) {
					Point positionLibre = planete
							.getEmplacementVoisinLibre(localite);
					if (positionLibre != null) {
						seReproduire(positionLibre);
					}
				}
			}
		}
	}

	public void setEnvironnement(Planete planete, Point localite) {
		this.planete = planete;
		this.localite = localite;
		planete.positionnerUneVie(this, localite);
	}

	public boolean estDuSexeOppose(Vie v) {
		if (this.getSexe() != v.getSexe()) {
			return true;
		} else {
			return false;
		}
	}

	public boolean estDuMemeClan(Vie v) {
		if (this.getClan() == v.getClan()) {
			return true;
		} else {
			return false;
		}
	}

	public boolean estDuClanOppose(Vie v) {
		if (this.getClan() != v.getClan()) {
			return true;
		} else {
			return false;
		}
	}

	public void seReproduire(Point position) {
		// TODO choisir le clan
		Vie nv = new Vie(clan);
		Statistique.plusUneVie(nv);
		nv.setEnvironnement(planete, position);
	}

	public boolean peutSeReproduire() {

		if (age >= AGE_MINI_REPRODUCTION && age <= AGE_MAX_REPRODUCTION) {
			return true;
		} else {
			return false;
		}
	}

	public boolean peutSeBattre() {

		if (age >= AGE_MINI_REPRODUCTION && age <= AGE_MAX_REPRODUCTION) {
			return true;
		} else {
			return false;
		}
	}

	public boolean estUneFemme() {
		if (sexe == SEXE_FEMME) {
			return true;
		} else {
			return false;
		}
	}

	public boolean estUnHomme() {
		if (sexe == SEXE_HOMME) {
			return true;
		} else {
			return false;
		}
	}

	public Color getColor() {
		if (estMort()) {
			return Color.black;
		} else if (clan == CLAN_A) {
			return new Color(age, 0, 0);
		} else if (clan == CLAN_B) {
			return new Color(0, age, 0);
		} else if (clan == CLAN_C) {
			return new Color(0, 0, age);
		} else {
			return Color.black;
		}
	}

	public boolean combattre(Vie v) {
		if (!v.peutSeBattre()) {
			return true;
		} else {
			if (Math.random() < 0.5) {
				return true;
			} else {
				return false;
			}
		}
	}
}
